package com.aisoar.magicsoar.api.service.impl;

import com.aisoar.magicsoar.api.common.base.ApiResult;
import com.aisoar.magicsoar.api.constant.PlayBookConstant;
import com.aisoar.magicsoar.storage.entity.Playbook;
import com.aisoar.magicsoar.storage.dto.PlaybookDTO;
import com.aisoar.magicsoar.storage.param.PlaybookParam;
import com.aisoar.magicsoar.storage.struct.PlaybookStruct;
import com.aisoar.magicsoar.storage.vo.PlaybookVO;
import com.aisoar.magicsoar.storage.dao.PlaybookDao;
import com.aisoar.magicsoar.api.service.PlaybookService;

import java.util.Objects;

import com.yomahub.liteflow.common.ChainConstant;
import com.yomahub.liteflow.parser.base.BaseXmlFlowParser;
import jakarta.annotation.Resource;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.springframework.stereotype.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.page.PageMethod;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import lombok.extern.slf4j.Slf4j;


/**
 * 剧本数据表(Playbook)表服务实现类
 *
 * @author makejava
 * @since 2025-07-01 13:43:23
 */
@Slf4j
@Service("playbookService")
public class PlaybookServiceImpl implements PlaybookService {
    @Resource
    private PlaybookDao playbookDao;
    @Resource
    private PlaybookStruct playbookStruct;


    /**
     * 通过XML格式的字符串直接创建剧本,加载成bean
     *
     * @param param 剧本创建参数
     */
    @Override
    public ApiResult<String> parseXmlPlayBook(PlaybookParam param) {

        try {

            Document document = DocumentHelper.parseText(param.getContent());

            Element e = document.getRootElement().element(ChainConstant.CHAIN);
            String chainId =
                    Optional.ofNullable(e.attributeValue(PlayBookConstant.ID)).orElse(e.attributeValue(PlayBookConstant.NAME));
            playbookDao.insert(new Playbook(PlayBookConstant.DEFAULT_VERSION, PlayBookConstant.DEFAULT_VERSION_DESC
                    , param.getContent(), param.getDesc(), chainId));
//            baseXmlFlowParser.parse(param.getContent());
            return ApiResult.buildSuccessResult("创建剧本成功，剧本id" + chainId);
        } catch (Exception e) {
            log.error("解析剧本失败");
            return ApiResult.buildFailResult(ApiResult.ResultCode.BAD_REQUEST, "剧本创建失败");
        }

    }


    /**
     * 通过ID查询单条数据
     *
     * @param param 入参
     * @return 实例对象
     */
    @Override
    public PlaybookVO queryById(PlaybookParam param) {
        Playbook record = this.playbookDao.queryById(param.getId());
        PlaybookVO vo = new PlaybookVO();
        if (Objects.nonNull(record)) {
            vo = this.playbookStruct.entityToVo(record);
        }
        return vo;
    }

    /**
     * 分页查询
     *
     * @param param 筛选条件
     * @return 查询结果
     */
    @Override
    public PageInfo<PlaybookVO> queryByPage(PlaybookParam param) {
        Page<PlaybookVO> page = PageMethod.startPage(param.getPageNo(), param.getPageSize());
        PlaybookDTO exam = playbookStruct.paramToDto(param);
        List<Playbook> list = this.playbookDao.queryAll(exam);
        List<PlaybookVO> voList = list.stream()
                .map(o -> this.playbookStruct.entityToVo(o))
                .collect(Collectors.toList());
        PageInfo<PlaybookVO> pageInfo = new PageInfo<>(page);
        pageInfo.setList(voList);
        return pageInfo;
    }

    /**
     * 条件查询
     *
     * @param dto 筛选条件
     * @return 查询结果
     */
    @Override
    public List<Playbook> queryAll(PlaybookDTO dto) {
        return this.playbookDao.queryAll(dto);
    }

    /**
     * 新增数据
     *
     * @param param 入参
     * @return 实例对象
     */
    @Override
    public boolean insert(PlaybookParam param) {
        Playbook exam = this.playbookStruct.paramToEntity(param);
        return this.playbookDao.insert(exam) > 0;
    }

    /**
     * 修改数据
     *
     * @param param 入参
     * @return 实例对象
     */
    @Override
    public boolean update(PlaybookParam param) {
        Playbook exam = this.playbookStruct.paramToEntity(param);
        return this.playbookDao.update(exam) > 0;
    }

    /**
     * 通过主键删除数据
     *
     * @param param 入参
     * @return 是否成功
     */
    @Override
    public boolean deleteById(PlaybookParam param) {
        Playbook exam = this.playbookStruct.paramToEntity(param);
        return this.playbookDao.removeById(exam) > 0;
    }
}
